package com.gxa.jjty.cjr.service.impl;

import com.gxa.jjty.cjr.mapper.AddressMapper;
import com.gxa.jjty.cjr.service.AddressService;
import com.gxa.jjty.core.common.ExceptionCode;
import com.gxa.jjty.core.common.JsonResult;
import com.gxa.jjty.core.exception.BusinessException;
import com.gxa.jjty.core.pojo.Address;
import com.gxa.jjty.core.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @author : 宸濯
 * Date: 2021/12/07 14:51
 * Description:
 * Version: V1.0
 */
@Service("addressService")
public class AddressServiceImpl implements AddressService {

    @Autowired(required = false)
    private AddressMapper addressMapper;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    public static final String ADDRESS_LIST ="addressList:";
    public static final String ADDRESS_KEY="addressPrimary:";

    @Override
    public Map<String, Object> insert(Address address) throws BusinessException {
        //判断是否为默认地址
        if (address.getAddressPrimary()){
            //是的话保存至redis
            redisTemplate.opsForValue().set(ADDRESS_KEY + address.getHandicappedId(), JsonUtil.objectToString(address));
        }
        int insert = addressMapper.insert(address);
        if (insert!=1){
            throw new BusinessException(ExceptionCode.ADD_DEFEATED);
        }
        redisTemplate.opsForList().leftPush(ADDRESS_LIST +address.getHandicappedId(),address);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("添加成功!");
        return jsonResult.getDataMap();
    }

    @Override
    public Map<String, Object> update(Address address) throws BusinessException {
        //判断修改的是否是默认地址
        if(address.getAddressPrimary()){
            //判断redis中是否存储了默认地址
            Object o = redisTemplate.opsForValue().get(ADDRESS_KEY + address.getHandicappedId());
            Address addressPrimary=null;
            if (o!=null){
                //redis存了默认地址则取出来
                addressPrimary=JsonUtil.stringToObject(o.toString(),Address.class);
            }else {
                //没存则从数据库取出来
                addressPrimary = addressMapper.selectAddressPrimary(address.getHandicappedId());
            }
            //用户原来没有默认地址,并且是默认地址判断是否与原来的默认地址相同
            if (addressPrimary!=null && (!addressPrimary.getId().equals(address.getId()))){
                //将原来的地址修改为非默认地址
                addressPrimary.setAddressPrimary(false);
                addressMapper.updateByPrimaryKey(addressPrimary);
                //将新的默认地址存到redis
                redisTemplate.opsForValue().set(ADDRESS_KEY + address.getHandicappedId(), JsonUtil.objectToString(address));
            }
        }
        int i = addressMapper.updateByPrimaryKey(address);
        if (i!=1){
            throw new BusinessException(ExceptionCode.UPDATE_FALSE);
        }
        redisTemplate.delete(ADDRESS_LIST +address.getHandicappedId());
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("修改成功!");
        return jsonResult.getDataMap();
    }

    @Override
    public Map<String, Object> delete(Long id) throws BusinessException {
        Address address = addressMapper.selectByPrimaryKey(id);
        if (address==null){
            throw new BusinessException(ExceptionCode.ADDRESS_NOT_EXIST);
        }
        int delete = addressMapper.delete(address);
        if (delete!=1){
            throw new BusinessException(ExceptionCode.DELETE_FALSE);
        }
        redisTemplate.delete(ADDRESS_LIST +address.getHandicappedId());
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("删除成功!");
        return jsonResult.getDataMap();
    }

    @Override
    public Map<String,Object> selectById(Long id) throws BusinessException {
        List<Object> range = redisTemplate.opsForList().range(ADDRESS_LIST + id, 0, -1);
        JsonResult jsonResult = new JsonResult();
        if (range==null || range.size()==0){
//            Example example = new Example(Address.class);
//            example.createCriteria().andEqualTo("handicappedId",id);
//            List<Address> addresses = addressMapper.selectByExample(example);
            List<Address> addresses = addressMapper.selectById(id);
            if (addresses==null || addresses.size()==0){
                throw new BusinessException(ExceptionCode.NODATA);
            }
            redisTemplate.opsForList().rightPushAll(ADDRESS_LIST +id,addresses.toArray());
            jsonResult.setData(addresses);
        }else {
            jsonResult.setData(range);
        }
        jsonResult.setCode("1");
        jsonResult.setMsg("查询成功!");
        return jsonResult.getDataMap();
    }

    @Override
    public Map<String, Object> selectAddressPrimary(Long id) throws BusinessException {
        Object o = redisTemplate.opsForValue().get(ADDRESS_KEY + id);
        Address address=null;
        if (o!=null){
            address= JsonUtil.stringToObject(o.toString(), Address.class);
        }else {
            address = addressMapper.selectAddressPrimary(id);
            if (address==null){
                throw new BusinessException(ExceptionCode.NODATA);
            }
            redisTemplate.opsForValue().set(ADDRESS_KEY + id, JsonUtil.objectToString(address));
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(address);
        return jsonResult.getDataMap();
    }
}
